import React, { Component } from 'react'
import Coupon from './Coupon'
import Style from './index.scss'

export default class CouponList extends Component {
	constructor(props) {
		 super(props);
		 this.state = {
		 	selectIndex: props.index > -1 ? (props.index*1) : -1
		 }
	}

    componentWillReceiveProps(nextProps){
		this.setState({
            selectIndex: nextProps.index > -1 ? (nextProps.index*1) : -1
		});
	}

	//返回的优惠券数据
	getData(data){
		//选中的优惠券
		this.setState({
			selectIndex: data.index
		});

		if(this.props.onClick){
			this.props.onClick(data); //传递给父组件
		}		
	}

	render() {
		//console.log(this.state.selectIndex, "this.state.selectIndex")
		return (
			<div className={Style.couponList}>
				{
					this.props.list.length > 0 ? this.props.list.map((item, i) => 
						<Coupon key={i} index={i} {...item} Selected={this.state.selectIndex === i ? true : false } onClick={(data) => this.getData(data)}/>
					) : ''
				}
			</div>
		)
	}
}

CouponList.defaultProps = {
	list : [
	]
}